package jdk7;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 Date类:
        public Date():获取当前时间的对象
        public long getTime():获取距离时间原点的毫秒值
        public Date(long time):把毫秒值转化为日期对象
        pubic void setTime(long time):设置日期对象时间为毫秒值对应的
 simpleDateFormat类: 日期格式化
        public simpleDateFormat():格式化为默认
        public simpleDateFormat(String pattern):指定格式
        format():把Date对象转为String;
        parse():把String转为Date

 */


        // 计算你来这个世界多少天
public class DateAndFormat {
    public static void main(String[] args) throws ParseException {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请按格式输入出生年月日:yyyy-mm-dd:");
        String birth=scanner.nextLine();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date bir = simpleDateFormat.parse(birth);
        Date today=new Date();
        long birTime = bir.getTime();
        long todayTime=today.getTime();
        long time=todayTime-birTime;
        int day= (int)(time/1000.0/60/60/24);
        System.out.println(day);
        /**尝试正则写法
        String regex="\\d{4}[-]\\d{1,2}[-]\\d{1,2}";
        Pattern pattern=Pattern.compile(regex);
        Matcher matcher=pattern.matcher(birth);
        if (matcher.find()){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(regex);

        }else {
            System.out.println("格式有误");
        }
        */

    }
}
